import Dexie, { Table } from 'dexie';
import { Player, Item, Skill, Quest, Enemy, Realm } from '@/types/game';

export class GameDatabase extends Dexie {
  players!: Table<Player>;
  items!: Table<Item>;
  skills!: Table<Skill>;
  quests!: Table<Quest>;
  enemies!: Table<Enemy>;
  realms!: Table<Realm>;

  constructor() {
    super('CultivationGameDB');
    this.version(1).stores({
      players: 'id, name, level, realm.level',
      items: 'id, name, type, rarity',
      skills: 'id, name, type, level, requiredRealm',
      quests: 'id, title, type, status',
      enemies: 'id, name, level',
      realms: 'id, name, level'
    });
  }
}

export const db = new GameDatabase();

// 初始化游戏数据
export const initializeGameData = async () => {
  // 检查是否已经初始化
  const realmCount = await db.realms.count();
  if (realmCount > 0) return;

  // 初始化境界数据
  const realms: Realm[] = [
    {
      id: 'qi-refining',
      name: '练气期',
      level: 1,
      description: '修仙入门境界，炼化天地灵气',
      requiredExp: 0,
      bonusStats: { health: 100, mana: 50, attack: 10, defense: 5, speed: 5 }
    },
    {
      id: 'foundation',
      name: '筑基期',
      level: 2,
      description: '筑建修仙根基，灵力更加凝实',
      requiredExp: 1000,
      bonusStats: { health: 200, mana: 100, attack: 20, defense: 15, speed: 10 }
    },
    {
      id: 'golden-core',
      name: '金丹期',
      level: 3,
      description: '凝结金丹，踏入修仙正途',
      requiredExp: 5000,
      bonusStats: { health: 500, mana: 250, attack: 50, defense: 30, speed: 20 }
    },
    {
      id: 'nascent-soul',
      name: '元婴期',
      level: 4,
      description: '元婴出窍，神通广大',
      requiredExp: 15000,
      bonusStats: { health: 1000, mana: 500, attack: 100, defense: 60, speed: 40 }
    }
  ];

  // 初始化技能数据
  const skills: Skill[] = [
    {
      id: 'basic-attack',
      name: '基础攻击',
      description: '最基本的攻击方式',
      type: 'attack',
      level: 1,
      maxLevel: 10,
      manaCost: 0,
      damage: 10,
      cooldown: 0,
      requiredRealm: 1
    },
    {
      id: 'qi-blast',
      name: '灵气冲击',
      description: '凝聚灵气进行攻击',
      type: 'attack',
      level: 1,
      maxLevel: 10,
      manaCost: 20,
      damage: 25,
      cooldown: 1,
      requiredRealm: 1
    },
    {
      id: 'healing-light',
      name: '回春术',
      description: '恢复生命值的治疗法术',
      type: 'heal',
      level: 1,
      maxLevel: 10,
      manaCost: 30,
      healAmount: 50,
      cooldown: 2,
      requiredRealm: 1
    }
  ];

  // 初始化物品数据
  const items: Item[] = [
    {
      id: 'wooden-sword',
      name: '木剑',
      description: '普通的木制长剑',
      type: 'weapon',
      rarity: 'common',
      stats: { attack: 15 },
      price: 100,
      stackable: false
    },
    {
      id: 'cloth-robe',
      name: '布袍',
      description: '简单的布制长袍',
      type: 'armor',
      rarity: 'common',
      stats: { defense: 10, health: 20 },
      price: 80,
      stackable: false
    },
    {
      id: 'health-potion',
      name: '回血丹',
      description: '恢复100点生命值',
      type: 'consumable',
      rarity: 'common',
      price: 50,
      stackable: true,
      maxStack: 99
    }
  ];

  // 初始化敌人数据
  const enemies: Enemy[] = [
    {
      id: 'wild-boar',
      name: '野猪',
      level: 1,
      health: 80,
      maxHealth: 80,
      attack: 12,
      defense: 5,
      speed: 8,
      skills: ['basic-attack'],
      rewards: {
        experience: 50,
        gold: 20,
        items: [
          { id: 'health-potion', quantity: 1, dropRate: 0.3 }
        ]
      }
    },
    {
      id: 'forest-wolf',
      name: '森林狼',
      level: 2,
      health: 120,
      maxHealth: 120,
      attack: 18,
      defense: 8,
      speed: 15,
      skills: ['basic-attack'],
      rewards: {
        experience: 80,
        gold: 35,
        items: [
          { id: 'health-potion', quantity: 1, dropRate: 0.4 }
        ]
      }
    }
  ];

  // 批量插入数据
  await db.realms.bulkAdd(realms);
  await db.skills.bulkAdd(skills);
  await db.items.bulkAdd(items);
  await db.enemies.bulkAdd(enemies);
};